package dk.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ReturnData.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.lang.UUID;
import dk.Utils.COSUtils;
import dk.beans.Examlist;
import dk.beans.subject;
import dk.beans.to.subjectlistToexamlist;
import dk.mapper.examlistmapper;
import dk.mapper.subjectmapper;

@RestController
public class SubjectController {
	@Autowired
	examlistmapper examlistmapper;
	@Autowired
	subjectmapper subjectmapper;
	//得到所有学科信息
	@RequestMapping("/GetSubject")
	public ResponseData GetSubject() {
		ResponseData data = new ResponseData();
		QueryWrapper<subject> qw = new QueryWrapper<subject>();
		List<subject> subjectlist = subjectmapper.selectList(null);
		data.setCode(200);
		data.setMessage("获取学科信息成功");
		data.setData(subjectlist);
		return data;
	}
	//根据分页获取学科信息
	@RequestMapping("GetSubjectByPage")
	public ResponseData GetSubjectBypage(Integer pagenum,Integer count) throws ParseException {
		//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//设计返回用封装map
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		ResponseData data = new ResponseData();
		IPage<subject> subjectPage = new Page<>(pagenum, count);//参数一是当前页，参数二是每页个数
		subjectPage = subjectmapper.selectPage(subjectPage, null);
		List<subject> list = subjectPage.getRecords();
		List<subjectlistToexamlist> list2= subjectmapper.selectquestionnum();
		
		for(subject subject : list) {
			int id = subject.getId();
			for(subjectlistToexamlist list3 :list2) {
				if(list3.getSubjectid() == id) {
					subject.setQuestionnum(list3.getQuestionnum());
				}
				else {
					subject.setQuestionnum(0);
				}
			}
			
			
			/*
			
			QueryWrapper<Examlist> qw = new QueryWrapper<Examlist>();
			qw.eq("subjectid", subject.getId());
			int count1 = examlistmapper.selectCount(qw);
			subject.setQuestionnum(count1);
			
			*/
			//Date time1 = new Date(format.format(subject.getCreatetime()));
			
			
			
		    }
		
		data.setCode(200);
		data.setMessage("分页查询subject成功");
		
		map.put("data",list);
		int dbcount = subjectmapper.selectCount(null);
		map.put("count",dbcount);
		data.setData(map);
		
		return data;
		
	}
	//获取所有学科信息的count
	@RequestMapping("GetSubjectCount")
	public ResponseData GetSubjectCount () {
		ResponseData data = new ResponseData();
		QueryWrapper<subject> qw = new QueryWrapper<subject>();
		int count = subjectmapper.selectCount(null);
		data.setData(count);
		data.setMessage("获取subject数据条数成功");
		data.setCode(200);
		return data;
		
	}
	//添加学科信息
	@RequestMapping("AddSubject")
	public ResponseData AddSubject(String name) {
		ResponseData data = new ResponseData();
		if(name != null) {
			//真正添加之前需要确定是否重复
			
			QueryWrapper<subject> qw1 = new QueryWrapper<subject>();
			qw1.eq("name", name);
			int count = subjectmapper.selectCount(qw1);
			if(count == 0) {
				QueryWrapper<subject> qw = new QueryWrapper<subject>();
				subject subject = new subject();
				subject.setName(name);
				subjectmapper.insert(subject);
				data.setCode(200);
				data.setMessage("添加subject成功");
				
			}
			else {
				data.setCode(201);
				data.setMessage("操作失败,学科名不可重复");
			}
			
		}
		else {
			data.setCode(202);
			data.setMessage("学科名不可为空");
		}
		
		
		
	    return data;
	}
	//根据学科名称查询指定学科
	@RequestMapping("/SearchSubjectListBySubjectNameByPage")
	public ResponseData SearchSubjectListBySubjectName(String name,Integer pagenum,Integer count) {
		//应使用分页查询 设置默认值
		if(pagenum == null) {
			pagenum = 1;
		}
		if(count == null) {
			count = 4;
		}
		ResponseData data = new ResponseData();
		IPage<subject> userPage = new Page<>(pagenum, count);
		QueryWrapper<subject> qw = new QueryWrapper<subject>();
		userPage = subjectmapper.selectPage(userPage, qw.like("name", name));
		
		//查询完查问题数量
		
		
		List<subject> selectList = userPage.getRecords();
		for(subject subject:selectList) {
			QueryWrapper<Examlist> qw1 = new QueryWrapper<Examlist>();
			qw1.eq("subjectid", subject.getId());
			int count1 = examlistmapper.selectCount(qw1);
			subject.setQuestionnum(count1);
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		data.setCode(200);
		data.setMessage("条件查询subject成功");
		
		map.put("data",selectList);
		int dbcount = subjectmapper.selectCount(qw);
		map.put("count",dbcount);
		data.setData(map);
		return data;
	}
	//根据学科id删除学科
	@RequestMapping("DeleteSubjectListById")
	public ResponseData DeleteSubjectListById(Integer id) {
		ResponseData data = new ResponseData();
		QueryWrapper<subject> qw = new QueryWrapper<subject>();
		qw.eq("id", id);
		//删除前查询是否含有题目
		QueryWrapper<Examlist> examlistqw = new QueryWrapper<Examlist>();
		int count = examlistmapper.selectCount(examlistqw.eq("id", id));
		if(count == 0) {
			subjectmapper.delete(qw);
			data.setMessage("删除成功");
			
			data.setCode(200);
		}
		else {
			data.setMessage("无法删除含有题目的学科");
			
			data.setCode(200);
		}
		
		return data;
	}
	//根据学科id查询学科详细信息
	@RequestMapping("GetSubjectById")
	public ResponseData GetSubjectById(Integer id) {
		ResponseData data = new ResponseData();
		
		QueryWrapper<subject> qw = new QueryWrapper<subject>();
		qw.eq("id", id);
		subject selectOne = subjectmapper.selectOne(qw);
		data.setCode(200);
		data.setMessage("根据id查询成功");
		data.setData(selectOne);
		return data;
	}
	//上传文件返回链接
	@RequestMapping("UploadSubjectImgById")
	public ResponseData  UploadSubjectImgById(Integer id,MultipartFile file) throws IOException {
		System.out.println("进入到上传模块");
		ResponseData data = new ResponseData();
		if(file.isEmpty()) {
			data.setCode(201);
			data.setMessage("文件为空,请重新上传");
			return data;
		}
		else {
			String fileName = file.getOriginalFilename();
			fileName = UUID.fastUUID()+fileName.substring(fileName.lastIndexOf("."));
			//这里上传到腾讯云 先转存到本地 之后上传 
			
			String path ="I://毕业开发缓存路径//"+id+"//imgs//"+fileName;
			
			File localfile = new File(path);
			//创建完毕 将接收到的前端file同步到本体路径
			FileUtils.copyInputStreamToFile(file.getInputStream(), localfile);
			//声明云端路径
			String CloudPath = "DK-powerful//"+id+"//imgs//"+fileName;
			//将文件上传
			int hash = COSUtils.uploadFileByPath(path, CloudPath);
			
			
			data.setCode(200);
			data.setMessage("上传成功");
			data.setData("https://misaka917-1301164487.cos.ap-nanjing.myqcloud.com/"+CloudPath);
			
		}
		return data;
		
		
	}
	//修改列表功能
	@RequestMapping("EditSubjectByid")
	public ResponseData EditSubjectById(Integer id,String url,String name) {
		subject subject = new subject();
		subject.setId(id);
		subject.setImg_url(url);
		subject.setName(name);
		ResponseData data = new ResponseData();
		subjectmapper.update(subject, new QueryWrapper<subject>().eq("id", id));
		data.setCode(200);
		data.setMessage("修改成功");
		return data;
		
	}
	
	
	
	
}
